package com.zhb.blog.web;


import com.zhb.blog.model.Tag;
import com.zhb.blog.model.Type;
import com.zhb.blog.service.BlogService;
import com.zhb.blog.service.TagService;
import com.zhb.blog.service.TypeService;
import com.zhb.blog.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.ArrayList;
import java.util.List;

@Controller
public class TagShowController {

    @Autowired
    TagService tagService;

    @Autowired
    BlogService blogService;

    @GetMapping("/tags/{id}")
    public String tags(Model model, @PathVariable Long id, @PageableDefault(size = 5,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable){
        //用listTagTop(int a)而不用listTag(..)的原因是它可以排序好标签
        List<Tag> tags = tagService.listTagTop(1000);
        //id表示当前用户选择的标签的id
        if(id == -1){
            id = tags.get(0).getId();
        }
        model.addAttribute("tags",tags);
        //关联查询blog表和tags表
        model.addAttribute("page",blogService.listBlog(id,pageable));
        model.addAttribute("activeTagId",id);
        return "tags";
    }
}
